package ua.edu.nuos.jeetraining2012.cms.util.mail.course;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import ua.edu.nuos.jeetraining2012.cms.util.LocaleWrapper;
import ua.edu.nuos.jeetraining2012.cms.util.mail.NotifyInstructor;

import java.io.IOException;
import java.io.StringWriter;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;

/**
 * Send notify to instructors about students leaving of course.
 *
 * @author Victor N. Ganichev victor.ganichev@gmail.com
 * @version 1.0 Date: 03.07.13 Time: 13:41
 * @since 1.0
 */
public class StudentUnenrolledFromCourse extends NotifyInstructor {

    /** Define student leaving reason. */
    private String unenrollReason;

    /** Define logger. */
    final private static Logger logger = org.slf4j.LoggerFactory.getLogger(StudentUnenrolledFromCourse.class);

    // Constructors.
    public StudentUnenrolledFromCourse() {
        logger.debug("StudentUnenrolledFromCourse invocation.");
        i18n = LocaleWrapper.getBundle("course");
    }

    // Getters & Setters
    public void setUnenrollReason(String unenrollReason) {
        this.unenrollReason = unenrollReason;
    }

    @Override
    public String getSubject() {
        try {
            logger.debug("Defining email subject.");
            MessageFormat formatter = new MessageFormat("");
            formatter.setLocale(LocaleWrapper.getLocale());
            formatter.applyPattern(i18n.getString("notify_instructor_unenroll_subject"));

            return formatter.format(new Object[] {
                    i18n.getString("notify_instructor_unenroll_subject"),
                    student.getName(),
                    course.getTitle()
            });

        } catch (MissingResourceException | NullPointerException e) {
            return "Інформація. Студент покинув курс.";
        }
    }

    @Override
    public String getBodyMessage() {
        Map<String, Object> dataMap = new HashMap<>();

        MessageFormat formatter = new MessageFormat("");
        formatter.setLocale(LocaleWrapper.getLocale());
        formatter.applyPattern(i18n.getString("notify_instructor_unenroll_body"));

        logger.debug("Forming notification mail about user {} enrollment to course {}", student.getId(), course.getId());
        try {
            dataMap.put("noticeBody", formatter.format(new Object[] {
                    i18n.getString("notify_instructor_unenroll_body"),
                    student.getName(),
                    course.getTitle(),
                    unenrollReason
            }));

            Configuration cfg = getTemplateConfig();
            Template template = cfg.getTemplate("notifyInstructorStudentUnenrolled.ftl");
            StringWriter writer = new StringWriter();
            template.process(dataMap, writer);
            return writer.toString();
        } catch (IOException | TemplateException |
                MissingResourceException | NullPointerException e) {
            logger.error("getBodyMessage() Exception", e);
        }
        return null;
    }
}
